package com.hxrfid.sms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxrfid.common.core.domain.BaseEntity;
import com.hxrfid.common.core.domain.PageQuery;
import com.hxrfid.common.core.page.TableDataInfo;
import com.hxrfid.common.utils.Assert;
import com.hxrfid.common.utils.StringUtils;
import com.hxrfid.sms.config.properties.SmsProperties;
import com.hxrfid.sms.core.AliyunSmsTemplate;
import com.hxrfid.sms.core.SmsTemplate;
import com.hxrfid.sms.core.TencentSmsTemplate;
import com.hxrfid.sms.domain.SysSmsConfig;
import com.hxrfid.sms.domain.bo.SysSmsConfigBo;
import com.hxrfid.sms.domain.vo.SysSmsConfigVo;
import com.hxrfid.sms.mapper.SysSmsConfigMapper;
import com.hxrfid.sms.service.ISysSmsConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 短信配置Service业务层处理
 *
 * @author hxrfid
 * @date 2022-12-26
 */
@RequiredArgsConstructor
@Service
public class SysSmsConfigServiceImpl extends ServiceImpl<SysSmsConfigMapper, SysSmsConfig> implements ISysSmsConfigService {

    private final SysSmsConfigMapper baseMapper;

    /**
     * 查询短信配置
     * @param id 主键ID
     * @return 短信配置
     */
    @Override
    public SysSmsConfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }
    /**
     * 查询短信配置
     * @param templateId 模板ID
     * @return 短信配置
     */
    @Override
    public SysSmsConfigVo queryByTemplateId(String templateId){
        LambdaQueryWrapper<SysSmsConfig> lqw = Wrappers.<SysSmsConfig>lambdaQuery().eq(SysSmsConfig::getTemplateId, templateId)
            .eq(SysSmsConfig::getDelFlag, "0");
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 构建短信配置
     * @param templateId 模板ID
     * @return 短信配置
     */
    @Override
    public SmsTemplate buildTemplate(String templateId) {
        SysSmsConfigVo vo = this.queryByTemplateId(templateId);
        Assert.notNull(vo, "配置不存在");
        SmsProperties smsProperties = new SmsProperties(vo.getEndpoint(), vo.getAccessKey(), vo.getSecretKey(),
            vo.getSignName(), vo.getTemplateId(), vo.getSdkAppId(), vo.getId());
        return "0".equals(vo.getType()) ? new AliyunSmsTemplate(smsProperties) : new TencentSmsTemplate(smsProperties);
    }

    /**
     * 查询短信配置列表
     * @param bo 短信配置对象
     * @param pageQuery 主键ID
     * @return 短信配置列表
     */
    @Override
    public TableDataInfo<SysSmsConfigVo> queryPageList(SysSmsConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysSmsConfig> lqw = buildQueryWrapper(bo);
        Page<SysSmsConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询短信配置列表
     * @param bo 短信配置对象
     * @return 短信配置列表
     */
    @Override
    public List<SysSmsConfigVo> queryList(SysSmsConfigBo bo) {
        LambdaQueryWrapper<SysSmsConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
    * 组装查询条件
    * @param bo 短信配置对象
    * @return 查询条件
    */
    private LambdaQueryWrapper<SysSmsConfig> buildQueryWrapper(SysSmsConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysSmsConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getEndpoint()), SysSmsConfig::getEndpoint, bo.getEndpoint());
        lqw.eq(StringUtils.isNotBlank(bo.getAccessKey()), SysSmsConfig::getAccessKey, bo.getAccessKey());
        lqw.eq(StringUtils.isNotBlank(bo.getSecretKey()), SysSmsConfig::getSecretKey, bo.getSecretKey());
        lqw.like(StringUtils.isNotBlank(bo.getSignName()), SysSmsConfig::getSignName, bo.getSignName());
        lqw.eq(StringUtils.isNotBlank(bo.getSdkAppId()), SysSmsConfig::getSdkAppId, bo.getSdkAppId());
        lqw.eq(StringUtils.isNotBlank(bo.getTemplateId()), SysSmsConfig::getTemplateId, bo.getTemplateId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), SysSmsConfig::getType, bo.getType());
        lqw.orderByDesc(BaseEntity::getCreateTime);
        return lqw;
    }

    /**
     * 新增短信配置
     * @param bo 短信配置对象
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean insertByBo(SysSmsConfigBo bo) {
        SysSmsConfig add = BeanUtil.toBean(bo, SysSmsConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改短信配置
     * @param bo 短信配置对象
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updateByBo(SysSmsConfigBo bo) {
        SysSmsConfig update = BeanUtil.toBean(bo, SysSmsConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     * @param entity 短信配置对象
     */
    private void validEntityBeforeSave(SysSmsConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除短信配置
     * @param ids 主键ID的list
     * @param isValid 是否业务校验
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
